import router from '@/router';

export type TRouterInfo = {
  meta: any;
  path: string;
  routeIdx: number;
  title: string;
  name?: string;
  // isAlive?: boolean;
  isHome?: boolean;
};

export type TTabRouterType = {
  isRefreshing: boolean;
  tabRouterList: Array<TRouterInfo>;
};

const homeRoute: Array<TRouterInfo> = [
  // {
  //   path: '/home/base',
  //   routeIdx: 0,
  //   title: '仪表盘',
  //   // name: 'DashboardBase',
  //   isHome: true,
  // },
];

const state: TTabRouterType = {
  tabRouterList: homeRoute,
  isRefreshing: false,
};

// 不需要做多标签tabs页缓存的列表 值为每个页面对应的name 如 DashboardDetail
// const ignoreCacheRoutes = ['DashboardDetail'];
const ignoreCacheRoutes = [];

const mutations = {
  // 处理刷新
  toggleTabRouterAlive(state: TTabRouterType, routeIdx: number) {
    state.isRefreshing = !state.isRefreshing;
    state.tabRouterList[routeIdx].meta.isAlive = !state.tabRouterList[routeIdx].meta.isAlive;
  },
  // 处理新增
  appendTabRouterList(state: TTabRouterType, newRoute: TRouterInfo) {
    // const needAlive = !ignoreCacheRoutes.includes(newRoute.name);
    if (!state.tabRouterList.find((route: TRouterInfo) => route.path === newRoute.path)) {
      state.tabRouterList = state.tabRouterList.concat([newRoute]);
    }
  },
  // 处理关闭当前
  subtractCurrentTabRouter(state: TTabRouterType, newRoute: TRouterInfo) {
    const {path, routeIdx} = newRoute;
    let idx = routeIdx;
    if (routeIdx === undefined) {
      // 通过path找routeIdx
      idx = state.tabRouterList.findIndex((val) => val.path === path)
    }
    const currentRouter = state.tabRouterList[idx];
    const newTabRouterList = state.tabRouterList.slice(0, idx).concat(state.tabRouterList.slice(idx + 1))
    if (newTabRouterList.length <= 0 && this.state.permission.currentSystemRouters?.children?.length > 0 && this.state.permission?.currentSystemRouters?.redirect) {
      let router = this.state.permission.currentSystemRouters.children[0]
      if (router?.children.length > 0) {
        [router] = router.children
      }
      // 增加当前子系统路由的第一个
      state.tabRouterList = [{
        path: this.state.permission.currentSystemRouters.redirect,
        routeIdx: 0,
        title: router.meta.title,
        name: router.name,
        meta: {...router.meta},
        isHome: false,
      }];
    } else {
      state.tabRouterList = newTabRouterList;
    }
    // 如果当前的路由是子页面则跳转父页面 t用于刷新keep-alive 内嵌
    if (currentRouter.meta.activeMenu) {
      let path = currentRouter.meta.activeMenu
      if (localStorage.getItem("historyPath") !== '/')
      {
        path = localStorage.getItem("historyPath")
      }
      router.push({
        path,
        query: {
          t: Date.parse(new Date().toString()).toString(),
        }
      })
    }
  },
  // 处理关闭右侧
  subtractTabRouterBehind(state: TTabRouterType, newRoute: TRouterInfo) {
    const {routeIdx} = newRoute;
    state.tabRouterList = state.tabRouterList.slice(0, routeIdx + 1);
  },
  // 处理关闭左侧
  subtractTabRouterAhead(state: TTabRouterType, newRoute: TRouterInfo) {
    const {routeIdx} = newRoute;
    // state.tabRouterList = homeRoute.concat(state.tabRouterList.slice(routeIdx));
    state.tabRouterList = [state.tabRouterList[0]].concat(state.tabRouterList.slice(routeIdx));
  },
  // 处理关闭其他
  subtractTabRouterOther(state: TTabRouterType, newRoute: TRouterInfo) {
    const {routeIdx} = newRoute;
    // state.tabRouterList = homeRoute.concat([state.tabRouterList?.[routeIdx]]);
    state.tabRouterList = [state.tabRouterList[0]].concat([state.tabRouterList?.[routeIdx]]);
  },
  removeTabRouterList(state: TTabRouterType, path: string) {
    // .replace("/","")
    const newRoute: Array<TRouterInfo> = path.includes("/home") ? [
      {
        path,
        routeIdx: 0,
        title: '仪表盘',
        // name: 'DashboardBase',
        isHome: true,
        meta: {
          isAlive: false // 首页不缓存
        }
      },
    ] : [];
    state.tabRouterList = newRoute;
  },
  initTabRouterList(state: TTabRouterType, newRoute: TRouterInfo[]) {
    state.tabRouterList = newRoute;
  },
  Refreshing(state: TTabRouterType) {
    state.isRefreshing = !state.isRefreshing;
  }
};

const getters = {
  tabRouterList: (state: TTabRouterType) => state.tabRouterList,
  isRefreshing: (state: TTabRouterType) => state.isRefreshing,
};

const actions = {};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
